package basic.study.wantOffer.chapter2;

/**
 * @author Kevin
 * @date 2020/5/2 20:42
 * 斐波那契数列
 */
public class Problem11 {

    //方法1：递归，不建议
    private static int getMethodNumber(int n) {
        if (n == 0) {
            return 0;
        } else if(n == 1) {
            return 1;
        } else if (n == 2) {
            return 2;
        } else {
            return getMethodNumber(n-2) + getMethodNumber(n-1);
        }
    }

    //方法2: 循环，建议
    private static long fibonacci(int n) {
        long[] a = {0, 1};
        if (n < 2) {
            return a[n];
        }
        long fib1 = 0;
        long fib2 = 1;
        long fibN = 0;
        for (int i = 2; i <= n; i++) {
            fibN = fib1 + fib2;
            fib1 = fib2;
            fib2 = fibN;
        }
        return fibN;
    }

    public static void main(String[] args) {
        System.out.println(fibonacci(5));
        System.out.println(getMethodNumber(10));
    }
}
